package xnj.lazydog.btcontroller.MyViews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import xnj.lazydog.btcontroller.R;

public class ToolView extends View {

    Paint p;
    float w,h;

   // String hex="";
   // String autoSend="";

    final float buttonRadiusRatio = 0.25f;
    final float buttonInnerRatio = 0.7f;
    final float textSizeRatio = 0.5f;

    public ToolView(Context context, AttributeSet set)
    {
        super(context,set);
        p=new Paint();
        p.setAntiAlias(true);
        p.setTextAlign(Paint.Align.LEFT);
        //w = getWidth();
        //h = getHeight();

        //setMeasuredDimension();
        w = getMeasuredWidth();
        h = getMeasuredHeight();

        Log.w("Size","w,h:"+w+","+h);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
//        float w,h;
//        w = getWidth();
//        h = getHeight();
//       // setMeasuredDimension((int)w,(int)h);
//    }


    float x(float ratio)
    {
        return w*ratio;
    }
    float y(float ratio)
    {
        return h*ratio;
    }

    float hexInnerPx = 0;
    float autoInnerPx = 0;

    static boolean isHex=false;
    static boolean isAutoSend=false;

    int loopMs = 1000;

    float hex_d,as_d;


    public void setAutoSend(boolean as)
    {
        isAutoSend = as;
        postInvalidate();
    }

    public void setHexMode(boolean hex)
    {
        isHex = hex;
        postInvalidate();;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        w = getMeasuredWidth();
        h = getMeasuredHeight();

        float buttonRadiusPx = y(buttonRadiusRatio);
        float textSizePx = y(buttonRadiusRatio);
        float buttonInnerPx = buttonRadiusPx*buttonInnerRatio;


//        if(autoDuration<10&&isAutoSend)
//            isAutoSend = false;


        if(isHex)
            hex_d = (buttonInnerPx-hexInnerPx);
        else
            hex_d=(-hexInnerPx);
        hexInnerPx+=hex_d*0.15f;

        if(Math.abs(hex_d)<0.5)
            hexInnerPx = isHex?buttonInnerPx:0;


        if(isAutoSend)
           as_d=(buttonInnerPx-autoInnerPx);
        else
            as_d=(-autoInnerPx);

        autoInnerPx+=as_d*0.15f;

        if(Math.abs(as_d)<0.5f)
            autoInnerPx = isAutoSend?buttonInnerPx:0;


        p.setTextSize(textSizePx);

        p.setARGB(255,155,155,155);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(buttonRadiusPx*0.15f);
        canvas.drawCircle(x(0.15f),y(0.5f),buttonRadiusPx,p);
        canvas.drawCircle(x(0.5f),y(0.5f),buttonRadiusPx,p);

        p.setColor(0xff64C5FF);
        p.setStyle(Paint.Style.FILL);
        p.setStrokeWidth(buttonRadiusPx*0.1f);
        if(hexInnerPx>5)
        canvas.drawCircle(x(0.15f),y(0.5f),hexInnerPx,p);

        if(autoInnerPx>5)
        canvas.drawCircle(x(0.5f),y(0.5f),autoInnerPx,p);

        p.setTextAlign(Paint.Align.LEFT);
        p.setARGB(255,75,75,75);
        canvas.drawText("HEX",x(0.18f)+buttonRadiusPx,y(0.5f)+textSizePx*0.4f,p);
        canvas.drawText("自动重发",x(0.53f)+buttonRadiusPx,y(0.5f)+textSizePx*0.4f,p);

        if(Math.abs(hex_d)>0.5||Math.abs(as_d)>0.5)
            invalidate();
    }

    public void setOnCheckedListener(OnCheckedListener listener)
    {
       onCheckedListener = listener;
    }


    public interface  OnCheckedListener{

        void onHexChecked(boolean state);
        void onAutoChecked(boolean state);

    }
    OnCheckedListener onCheckedListener;

    float px,py;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        px = event.getX();
        py = event.getY();

        if(event.getAction()==MotionEvent.ACTION_DOWN)
        {

            if(px<x(0.35f))
            {
             isHex = !isHex;
             if(onCheckedListener!=null)
                 onCheckedListener.onHexChecked(isHex);

            }else if(px<x(0.8f))
            {
                isAutoSend =!isAutoSend;
                if(onCheckedListener!=null)
                    onCheckedListener.onAutoChecked(isAutoSend);
            }
            postInvalidate();
        }
        return true;
    }
}
